/**
 * @author KujoMiva
 * @name Loading.ts
 * @desc 过渡界面
 * @time 2020年02月11日 16:45:34 星期二
 */
class Loading extends egret.DisplayObjectContainer {
  private animationSpeed = 800
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.getUserInfo()
    this.createWing()
    this.createHeart()
    this.createLoadingTip()
  }

  private createHeart() {
    const heart = Utils.createBitmapByName('item_heart_png')
    Utils.createScale(heart, this, .15)
    Utils.anchorCenter(heart)
    heart.x = this.stage.stageWidth / 2
    heart.y = this.stage.stageHeight / 2
    egret.Tween.get(heart, { loop: true })
      .to({ scaleX: heart.scaleX - .02, scaleY: heart.scaleY - .02 }, 60000 / 144)
      .to({ scaleX: heart.scaleX, scaleY: heart.scaleY }, 60000 / 144)
      .wait(60000 / 144)

    heart.touchEnabled = false
    heart.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      this.stage.dispatchEvent(new egret.Event(ParamsPool.LOADING_GOTO))
      egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
    }, this)

    if (ParamsPool.LOADING_GOTO === 'gotoOver') {
      egret.Tween.get(this).wait(2000).call(() => {
        this.stage.dispatchEvent(new egret.Event(ParamsPool.LOADING_GOTO))
        egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
      })
    }

    if (ParamsPool.LOADING_GOTO === 'gotoCheckMap') {
      egret.Tween.get(this)
        .call(() => {
          this.dispatchEvent(new egret.Event('hideWing'))
          egret.Tween.removeTweens(heart)
          egret.Tween.get(heart).wait(3000).to({ scaleX: .05, scaleY: .05, x: 320, y: 693 }, 1000)
        })
        .wait(4000).call(() => {
          this.stage.dispatchEvent(new egret.Event(ParamsPool.LOADING_GOTO))
          egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
        })
    }

    if (ParamsPool.LOADING_GOTO === 'gotoCheckText') {
      egret.Tween.get(this).wait(2000).call(() => {
        this.stage.dispatchEvent(new egret.Event(ParamsPool.LOADING_GOTO))
        egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
      })
    }

  }

  private createWing() {
    const container = new egret.Sprite()
    const item_r = Utils.createBitmapByName('item_heart_r_png')
    container.addChild(item_r)
    const item_l = Utils.createBitmapByName('item_heart_l_png')
    container.addChild(item_l)

    Utils.createScale(container, this, .15)
    Utils.anchorCenter(container)
    container.x = this.stage.stageWidth / 2
    container.y = 580

    item_r.anchorOffsetY = item_r.height / 2
    item_l.anchorOffsetY = item_l.height / 2
    
    egret.Tween.get(item_r)
      .to({ x: item_r.width * .8 }, this.animationSpeed / 2)
      .call(() => {
        egret.Tween.get(item_r, { loop: true })
          .to({ rotation: -15 }, this.animationSpeed / 4)
          .to({ rotation: 0 }, this.animationSpeed / 4)
          .to({ rotation: 15 }, this.animationSpeed / 4)
          .to({ rotation: 0 }, this.animationSpeed / 4)
      })
    egret.Tween.get(item_l)
      .to({ anchorOffsetX: item_l.width * .8 }, this.animationSpeed / 2)
      .call(() => {
        egret.Tween.get(item_l, { loop: true })
          .to({ rotation: 15 }, this.animationSpeed / 4)
          .to({ rotation: 0 }, this.animationSpeed / 4)
          .to({ rotation: -15 }, this.animationSpeed / 4)
          .to({ rotation: 0 }, this.animationSpeed / 4)
      })

    this.addEventListener('hideWing', () => {
      egret.Tween.get(container).wait(2000).to({ alpha: 0 }, this.animationSpeed)
    }, this)

  }

  private createLoadingTip() {
    const text = Utils.createText(ParamsPool.LOADING_TIP, { x: 0, y: 1080 }, this)
    text.stroke = 0
    text.size = 20
    text.bold = false
    text.textColor = 0xffffff
    text.width = this.stage.stageWidth
    text.textAlign = egret.HorizontalAlign.CENTER
    text.alpha = 0
    egret.Tween.get(text).to({ alpha: 1 }, this.animationSpeed)
  }


  private getUserInfo() {
    if (!$('#avatar').html()) {
      window.location.href = "http://www.unmcc.com/unmcc/miva/LZ_OLG" + "?" + Math.random();
      return;
    }
    ParamsPool.USER_AVATAR = $('#avatar').html()
    console.log($('#avatar').html())
    $.get('http://39.104.181.160:7489/H5').then(res => {
      console.log(res)
      // this.show_loading_text++
      ParamsPool.PLAYER_NUM = res.data.count
      ParamsPool.PLAYER_LIST = res.data.rows
    })
  }

}